package com.harmontronics.hanma.paas.paas.interceptor;

import com.bstek.ureport.Utils;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmDatasource;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmDatasourceEnv;
import com.harmontronics.hanma.paas.paas.domain.app.entity.valueobject.DataBaseUrlEnum;
import com.harmontronics.hanma.paas.paas.domain.app.entity.valueobject.ViewDataBaseTypeEnum;
import com.harmontronics.hanma.paas.paas.domain.hmureport.service.UreportDataSource;
import com.harmontronics.hanma.paas.paas.service.HmDatasourceAppService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class AppReportDataSourceInterceptor extends HandlerInterceptorAdapter {

    private static Utils utils = new Utils();

    @Autowired
    private HmDatasourceAppService hmDatasourceAppService;

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        ConfigurableApplicationContext context = (ConfigurableApplicationContext)applicationContext;
        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)context.getBeanFactory();

        List<HmDatasource> datasourceList = hmDatasourceAppService.getAll();

        if(CollectionUtils.isNotEmpty(datasourceList)){
            for(HmDatasource datasource : datasourceList){
                String key = datasource.getKey();

                ViewDataBaseTypeEnum viewDataBaseTypeEnum = datasource.getType();
                String driver = viewDataBaseTypeEnum.getDriver();
                List<HmDatasourceEnv> envList = datasource.getEnvList();
                if(CollectionUtils.isNotEmpty(envList)){
                    for(HmDatasourceEnv env : envList){
                        String host = env.getHost();
                        Integer port = env.getPort();
                        String username = env.getUsername();
                        String password = env.getPassword();
                        String databaseName = env.getDatabaseName();
                        String preUrl = DataBaseUrlEnum.of(viewDataBaseTypeEnum.getCode()).getUrl();
                        String url = preUrl + host+":"+port + "/" + databaseName;


                        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(
                                UreportDataSource.class);
                        beanDefinitionBuilder.addPropertyValue("name", key);
                        beanDefinitionBuilder.addPropertyValue("username", username);
                        beanDefinitionBuilder.addPropertyValue("password", password);
                        beanDefinitionBuilder.addPropertyValue("url", url);
                        beanDefinitionBuilder.addPropertyValue("driver", driver);

                        beanFactory.registerBeanDefinition(
                                key, beanDefinitionBuilder.getBeanDefinition());
                    }
                }
            }

            utils.setApplicationContext(applicationContext);
        }


        /*
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(
                UreportDataSource.class);

        beanDefinitionBuilder.addPropertyValue("name", "mydatasource1");
        beanDefinitionBuilder.addPropertyValue("username", "dev");
        beanDefinitionBuilder.addPropertyValue("password", "hcol_2020");
        beanDefinitionBuilder.addPropertyValue("url", "jdbc:postgresql://10.19.1.128:5432/dev_hanma");
        beanDefinitionBuilder.addPropertyValue("driver", "org.postgresql.Driver");

        beanFactory.registerBeanDefinition("mydatasource1", beanDefinitionBuilder.getRawBeanDefinition());

         */

        return true;
    }

}
